﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.IO;
using PMSA.Framework.Log;

/// <summary>
/// Summary description for AdminNameVariables
/// </summary>
public class AdminCore
{
    public static string SessionUserName = "AdminUserName";
    public static string PageLogin = "~/Admin/";
    public static string PageLogout = "~/Admin/Logout.aspx";
    public static string PageDefaultWhenLoggedIn = "~/Admin/Pages/Product/ProductManager.aspx";
    public static bool IsAuthenticate(HttpSessionState session)
    {
        bool ret = false;
        if (session[SessionUserName] != null)
        {
            ret = true;
        }

        ret = true;

        return ret;
    }
    public static string upload(HttpContext context, string path)
    {
        //path = "../../../Upload/Image/News";
        string ret = "";
        String filename = HttpContext.Current.Request.Headers["X-File-Name"];

        if (string.IsNullOrEmpty(filename) && HttpContext.Current.Request.Files.Count <= 0)
        {
            ret = "{success:false}";
        }
        else
        {
            string mapPath = HttpContext.Current.Server.MapPath(path);
            if (Directory.Exists(mapPath) == false)
            {
                Directory.CreateDirectory(mapPath);
            }
            if (filename == null)
            {
                //This work for IE
                try
                {
                    HttpPostedFile uploadedfile = context.Request.Files[0];
                    filename = DateTime.Now.ToString("yyyyMMddhhmmss");
                    filename += uploadedfile.FileName;
                    uploadedfile.SaveAs(mapPath + "\\" + filename);
                    ret = "{success:true, name:\"" + filename + "\", path:\"" + path + "/" + filename + "\"}";
                }
                catch (Exception ex)
                {
                    CLogManager.Write("", ex.ToString(), "upload");
                    ret = "{success:false}";
                }
            }
            else
            {
                //This work for Firefox and Chrome.
                filename = DateTime.Now.ToString("yyyyMMddhhmmss") + filename;
                FileStream fileStream = new FileStream(mapPath + "\\" + filename, FileMode.OpenOrCreate);
                try
                {
                    Stream inputStream = HttpContext.Current.Request.InputStream;
                    using (BinaryReader reader = new BinaryReader(inputStream))
                    {
                        using (BinaryWriter writer = new BinaryWriter(fileStream))
                        {
                            int i = 0;
                            byte[] buffer = new byte[32768];

                            while ((i = reader.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                writer.Write(buffer, 0, i);
                            }
                        }
                    }
                    ret = "{success:true, name:\"" + filename + "\", path:\"" + path + "/" + filename + "\"}";
                }
                catch (Exception ex)
                {
                    ret = "{success:false}" + ex.ToString();
                }
                finally
                {
                    fileStream.Close();
                }
            }
        }
        return ret;
    }
}